package org.phoenixframework.api.rbac.util;

import com.alibaba.fastjson.JSONObject;
import org.phoenixframework.common.entity.SysAuthority;
import org.phoenixframework.common.tools.Constants;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelifeke
 * Email: hautxxxyzjk@163.com
 * DateTime: 2021/5/8 19:26
 * Description:
 */
public class AuthorityUtil {

    public static List<SysAuthority> build(List<SysAuthority> sysAuthorities) {
        List<SysAuthority> trees = new ArrayList<>();
        for (SysAuthority sysAuthority : sysAuthorities) {
            if (Constants.GOD.equals(sysAuthority.getParentId())) {
                sysAuthority.setLevel(1);
                trees.add(findChildren(sysAuthority, sysAuthorities));
            }
        }
        return trees;
    }

    private static SysAuthority findChildren(SysAuthority treeNode, List<SysAuthority> treeNodes) {
        treeNode.setChildren(new ArrayList<>());
        for (SysAuthority it : treeNodes) {
            if (treeNode.getId().equals(it.getParentId())) {
                int level = treeNode.getLevel() + 1;
                it.setLevel(level);
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public static List<JSONObject> buildByVue(List<SysAuthority> treeNodes) {
        List<JSONObject> menus = new ArrayList<>();
        if (treeNodes.size() == 1) {
            SysAuthority topNode = treeNodes.get(0);
            //左侧一级菜单
            List<SysAuthority> oneMeunList = topNode.getChildren();
            for (SysAuthority one : oneMeunList) {
                JSONObject oneMeun = new JSONObject();
                oneMeun.put("path", one.getPath());
                oneMeun.put("component", one.getComponent());
                oneMeun.put("redirect", "noredirect");
                oneMeun.put("name", "name_" + one.getId());
                oneMeun.put("hidden", false);

                JSONObject oneMeta = new JSONObject();
                oneMeta.put("title", one.getName());
                oneMeta.put("icon", one.getIcon());
                oneMeun.put("meta", oneMeta);

                List<JSONObject> children = new ArrayList<>();
                List<SysAuthority> twoMeunList = one.getChildren();
                for (SysAuthority two : twoMeunList) {
                    JSONObject twoMeun = new JSONObject();
                    twoMeun.put("path", two.getPath());
                    twoMeun.put("component", two.getComponent());
                    twoMeun.put("name", "name_" + two.getId());
                    twoMeun.put("hidden", false);

                    JSONObject twoMeta = new JSONObject();
                    twoMeta.put("title", two.getName());
                    twoMeun.put("meta", twoMeta);

                    children.add(twoMeun);

                    List<SysAuthority> threeMeunList = two.getChildren();
                    for (SysAuthority three : threeMeunList) {
                        if (StringUtils.isEmpty(three.getPath())) {
                            continue;
                        }

                        JSONObject threeMeun = new JSONObject();
                        threeMeun.put("path", three.getPath());
                        threeMeun.put("component", three.getComponent());
                        threeMeun.put("name", "name_" + three.getId());
                        threeMeun.put("hidden", true);

                        JSONObject threeMeta = new JSONObject();
                        threeMeta.put("title", three.getName());
                        threeMeun.put("meta", threeMeta);

                        children.add(threeMeun);
                    }
                }
                oneMeun.put("children", children);
                menus.add(oneMeun);
            }
        }
        return menus;
    }
}
